<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Information.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Menusinfo.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Products.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/Page.php";
require_once $_SERVER['DOCUMENT_ROOT'] . "/public/News.php";

/**
 * 页面入口
 * Class Cms
 */
class Cms
{
    public $category;
    public $current;
    public $is_html = false;

    function index()
    {

        if ($this->is_html) {
            header("location:./html/index.html");
        } else {
            include($_SERVER['DOCUMENT_ROOT'] . "/view/index.php");
        }
    }

    /**
     * 获取版权信息
     */
    function copyright()
    {
        $data = $this->header();
        return $data["copyright"];
    }

    /**
     * @return mixed
     */
    function header()
    {
        $information = new Information();
        $sysinfo = $information->getDefaultSystemInfo();
        return $sysinfo;
    }

    /**
     * 菜单信息
     * @param string $parent_id 父节点
     * @param int $id 主键
     * @return array|mixed
     */
    function menus($parent_id = 0, $id = 0)
    {
        $menusinfo = new Menusinfo();
        $condition = ["id_menuinfo" => $parent_id];
        $data = $menusinfo->findOne($condition);
        $this->category = $data;
        $condition = ["id_menuinfo" => $id];
        $data2 = $menusinfo->findOne($condition);
        $this->current = $data2;
        return $menusinfo->loadMenus($parent_id);
    }

    /**
     * 获取首页推荐的产品
     * @param int $type 类别 1 首页推荐  2 内页推荐
     * @return array|mixed
     */
    function products($type = 1)
    {
        $prod = new Products();
        $condition = ["index_show" => $type];
        $orderBy = ["created_date" => "desc"];
        return $prod->findList($condition, $orderBy);
    }

    /**
     * 首页关于我们
     * @return mixed
     */
    function about()
    {
        $about = new Page();
        $prefix = $about->getPrefix();
        $sql = "select a.* from " . $prefix . "pages a," . "$prefix" . "menuinfo b where a.id_data_category=b.id_menuinfo and b.menu_type='about'";
        return $about->execute($sql)[0];
    }

    /**
     * 获取新闻信息
     * @param $type 分类编号
     * @return array|mixed
     */
    function news($type)
    {
        if (!$type) {
            die("分类编号不能为空");
        }
        $news = new News();
        $menusinfo = new Menusinfo();
        $condition = ["id_data_category" => $menusinfo->getCategoryIdByType($type), "index_show" => 1];
        $orderBy = ["created_date" => "desc"];
        return $news->findByPage(1, 10, $condition, $orderBy);
    }

    /**
     *
     * 生成网站地图
     */
    function sitemap()
    {

    }

    /**
     * 将菜单url解析为菜单类型
     * @param $menu_url
     * @return false|string
     */
    public function menuUrlParse($menu_url)
    {
        $temp_url = explode("/", $menu_url);
        $temp_name = $temp_url[count($temp_url) - 1];
        $temp_name = substr($temp_name, 0, strlen($temp_name) - 4);
        return $temp_name;
    }
}